/**
 * Multicast project Bidois Morgan & Thomas Nunes
 *
 */
package middleware;

import Engine.Flag;
import Test.I_RecvMsgHandler;
import Engine.NioEngine;
import Engine.TypeMessage;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;

/**
 *
 * @author thomas
 */
public class MulticastManager extends Thread {

    protected NioEngine engine;
    protected Directory directory;
    protected int idClient;
    protected String name;

    public MulticastManager() {
        this.directory = new Directory();
        this.engine = new NioEngine();

        //We get the id of the JVM to identifie the client.
        String idProcessClient = ManagementFactory.getRuntimeMXBean().getName();
        String[] parts = idProcessClient.split("@");
        String strIdClient = parts[0];
        this.idClient = Integer.parseInt(strIdClient);
        System.out.println("strIdClient = " + strIdClient);
    }

    public void send2All(byte[] data, TypeMessage type) {
        engine.send2All(data, type, idClient);
    }

    @Override
    public void run() {
        engine.mainloop();
    }

    public void InitializeAsServer(InetAddress hostAddress, int port, I_RecvMsgHandler handler, I_ServerMulticastHandler action) throws IOException {
        engine.InitializeAsServer(hostAddress, port, handler, action);
    }

    public void InitializeAsClient(InetAddress hostAddress, int port, I_RecvMsgHandler handler, I_ClientMulticastHandler action) throws IOException {
        engine.InitializeAsClient(hostAddress, port, handler, action);
    }

    /**
     * Method use to send our Id to the server to register it. 
     * Do after the connect
     * Call in ExecuteClient- method register
     * @param name the name of the client
     */
    public void registerAsClient(String name) {
        this.name = name;
        String strId = "" + idClient;
        //TODO ajouter le nom à l'envoi et reception pour qu'il soit présent
        //dans l'annuaire et visible avec le who. enjoy...
        this.engine.send(strId.getBytes(), TypeMessage.SEND_ID, idClient);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("sleep exception.");
        }
       // this.send(("=" + name + "= est maintenant connecté=").getBytes());
    }
    
    /**
     * send a simple message to the client
     * @param data the message
     */
    public void send(byte[] data) {
        engine.send(data, TypeMessage.NORMAL, idClient);
    }

    /**
     * send a message to a client identifed by his id.
     * @param idClient the receiver
     * @param data the message
     */
    public void send(int idClient, byte[] data) {
        SocketChannel clientChannel = this.directory.getClient(idClient).getSocketChannel();
        engine.send(clientChannel, data, TypeMessage.NORMAL, idClient);
    }

    public void terminate() {
        engine.terminate();
    }
}
